$(document)
		.ready(
				function() {
					var id = getQueryString('id');
					console.log(id);

					$
							.ajax({
								type : "post",
								url : "articleOne",
								async : true,
								data : {
									"id" : id
								},
								dataType : 'json',
								success : function(data) {
									console.log(data)
									var articlePath = 'myAssets//img//article//';// 文章文件路径
									var facePath = 'myAssets//img//face//';// 头像文件路径
									var id = data.articleVo.id;// 文章id
									var type = data.articleVo.type;// 类型
									var title = data.articleVo.title;// 标题
									var content = data.articleVo.content;// 内容
									var date = data.articleVo.date;// 发布时间
									var startDate = data.articleVo.startDate;// 活动开始时间
									var endDate = data.articleVo.endDate;// 活动结束时间
									var address = data.articleVo.address;// 地址
									var joinUserNum = data.articleVo.joinUserNum;// 参加人数int
									var goodArticle = data.articleVo.goodArticle;// 点赞user
									// 集合
									var discusses = data.articleVo.discusses;// 评论
									// 集合
									var newdate = date.substring(0, 10);// 截取出发布日期
									// 取出掉时间

									var newstartDate = startDate.substring(10,
											5).replace("-", "月")
											+ '日';// 报名开放日期
									var newendDate = endDate.substring(10, 5)
											.replace("-", "月")
											+ '日';// 报名结束日期

									var newstartDateSSS = startDate.slice(11);// 报名开放时间
									// 12:30
									var newendDateSSS = endDate.slice(11);// 报名结束时间
									// 20:30
									console.log('截取后的格式' + newstartDate)
									console.log('截取后的格式' + newendDate)

									if (newstartDate == newendDate) {
										$("#startDate").html(
												newstartDate + ' '
														+ newstartDateSSS + '-'
														+ newendDateSSS);

									} else {
										$("#startDate").html(
												newstartDate + '-' + newendDate
														+ ' ' + newstartDateSSS
														+ '-' + newendDateSSS);
									}

									var myFiles = data.articleVo.myFiles;// 文件
									for (var i = 0; i < myFiles.length; i++) {
										var myFilesname = myFiles[i].name;
										// $('#myfile').innerHTML('<img
										// src="'+servicePath+myFilesname+'">');
										$("#myfile").append(
												'<img src="' + articlePath
														+ myFilesname + '">');
										// alert(myFilesname);
									}

									if (type == 'yule') {
										$("#type").html('分类：娱乐');
									}
									if (type == 'xueshu') {
										$("#type").html('分类：学术');
									}
									if (type == 'jingsai') {
										$("#type").html('分类：竞赛');
									}
									if (type == 'shetuan') {
										$("#type").html('分类：社团');
									}
									if (type == 'qita') {
										$("#type").html('分类：其他');
									}

									$("#title").html(title);// 标题
									$("#address").html('活动地址：' + address);// 地址
									$("#content").html(content);// 内容
									$("#date").html('发表于：' + newdate);// 发布时间
									$("#startDate").html();// 参加开放时间

									$("#user-name")
											.html(data.articleVo.user.name);// 参加开放时间
									$("#user-myfile")
											.html('<a href=heUser?id='+data.articleVo.user.id+'> <img src="'+ facePath+ data.articleVo.user.myFile.name+ '" alt=""></a>');// 参加开放时间
									if (data.articleVo.user.phoneNum == null
											|| data.articleVo.user.phoneNum == '') {
										$("#user-phoneNum")
												.html(
														'联系电话：<a href="tel:xxx-xxxxx-xxx">xxx-xxxxx-xxx</a>');// 联系电话
									} else {
										$("#user-phoneNum")
												.html(
														'联系电话：<a href="tel:'
																+ data.articleVo.user.phoneNum
																+ '">'
																+ data.articleVo.user.phoneNum
																+ '</a>');// 联系电话
									}

									var joinUser = data.articleVo.joinUser;// 参加User
									// 集合~~~~~~~~~~~~~~~~~~~~~~~~
									if (joinUser.length <= 3) {
										for (var i = 0; i < joinUser.length; i++) {
											var joinUserFileName = joinUser[i].myFile.name;
											$("#joinUser").append(
													'	<img src="' + facePath
															+ joinUserFileName
															+ '" alt="">');
										}
									}
									if (joinUser.length > 3) {
										$("#joinUser").html();
										for (var i = 0; i < 3; i++) {
											var joinUserFileName = joinUser[i].myFile.name;
											$("#joinUser").append(
													'	<img src="' + facePath
															+ joinUserFileName
															+ '" alt="">');
										}
										var joinUserOff3 = joinUser.length - 3;
										$("#joinUser")
												.append(
														'<div class="pet_zlnr_user_list_l_num">+'
																+ joinUserOff3
																+ '</div><div class="pet_zlnr_user_list_l_num_font"></div>');
									}

									$("#joinUserNum").html(
											'<a href="###">报名人数：'
													+ joinUser.length + '/'
													+ joinUserNum + '人</a>');// 参加人数

									for (var i = 0; i < joinUser.length; i++) {
										if (joinUser[i].id == data.userid) {
											$('#join-but')
													.html(
															'<button type="button"   class="am-btn am-btn-default  pet_zlnr_user_list1 "  style="background-color: #a5a5a5; border-radius :1000px; font: "黑体"; font-size: 18px; color: #FEFFFF;">已参加</button>');
										}
									}
									console.log('for开始')
									var discusses = data.articleVo.discusses;
									if(discusses.length==0){
										$("#discusses-type").html('暂时无人评论');
									}
									if(discusses.length!=0){
										$("#discusses-type").html('精彩评论:  '+discusses.length+'条');
									}
									
									for (var x = 0; x < discusses.length; x++) {
										var userid = discusses[x].thisUser.id;
										var userName = discusses[x].thisUser.name;
										var discussesid = discusses[x].id;
										var dateProbably = getDateDiff(discusses[x].date
												.replace("T", " "));
										var content = discusses[x].content;
										var usetFile = facePath
												+ discusses[x].thisUser.myFile.name;
										var goodLength = discusses[x].goodDiscuss.length;
										
										var goalDiscussHashSetlength = discusses[x].goalDiscussHashSet.length;
										
										if (goodLength == 0) {
											goodLength = '';
										}
										if (goalDiscussHashSetlength == 0) {
											goalDiscussHashSetlength = '';
										}
										if (x == 0) {
											$("#discusses-list")
													.append(
															'	<div class="pet_comment_list_block"><a href="heUser?id='
																	+ userid
																	+ '"><div class="pet_comment_list_block_l"><img src="'
																	+ usetFile
																	+ '" alt=""></div>	<a /><div class="pet_comment_list_block_r"><a href="heUser?id='
																	+ userid
																	+ '"><div class="pet_comment_list_block_r_info">'
																	+ userName
																	+ '</div>	<a /><div class="pet_comment_list_block_r_text">'
																	+ content
																	+ '</div><div class="pet_comment_list_block_r_bottom">	<div class="pet_comment_list_bottom_info_l"style="color: #888888;">'
																	+ dateProbably
																	+ '</div>	<div class="pet_comment_list_bottom_info_r"><a href="#" > <span><i class="iconfont"><img src="myAssets/img/16-16/赞.png"></i>'
																	+ goodLength
																	+ '	</span>	</a> <a href="goalDiscussHashList?id='
																	+ discussesid
																	+ '"><span>'
																	+ goalDiscussHashSetlength
																	+ ' 回复</span></a>	</div>	</div>	</div>	</div>');
										} else {
											$("#discusses-list")
													.append(
															'<div class="pet_comment_list_block"><a href="heUser?id='
																	+ userid
																	+ '"><div class="pet_comment_list_block_l">	<img src="'
																	+ usetFile
																	+ '" alt="">	</div>	</a>	<div class="pet_comment_list_block_r">	<a href="heUser?id='
																	+ userid
																	+ '"><div class="pet_comment_list_block_r_info" style="color: #888888;">'
																	+ userName
																	+ '</div></a>	<div class="pet_comment_list_block_r_text">'
																	+ content
																	+ '</div><div class="pet_comment_list_block_r_bottom"><div class="pet_comment_list_bottom_info_l">'
																	+ dateProbably
																	+ '</div>	<div class="pet_comment_list_bottom_info_r">	<a href="###"> <span><i class="iconfont"><img src="myAssets/img/16-16/赞.png"></i>	'
																	+ goodLength
																	+ '</span>	</a> <a href="goalDiscussHashList?id='
																	+ discussesid
																	+ '"> <span>'
																	+ goalDiscussHashSetlength
																	+ ' 回复</span></a>	</div>	</div>	</div>	</div>');
										}

										if (x == 4) {
											$("#if-list-discusses").html('<hr><div class="am_news_load"><span><a href="discussListOfArticle?id='+data.id+'"> 查看更多评论</a></span></div>');
											break;
										}
									 }

								},
								error : function() {
								}
							});

				})

// 获取url中的参数
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); // 匹配目标参数
	var result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数
	if (result != null) {
		return decodeURIComponent(result[2]);
	} else {
		return null;
	}
}

$(function() {
	$('#doc-prompt-toggle').on('click', function() {
		$('#my-prompt').modal({
			relatedTarget : this,
			onConfirm : function(e) {
				var id = getQueryString('id');
				$.ajax({
					type : "post",
					url : "articleJoinAdd",
					async : true,
					data : {
						'id' : id,
					},
					dataType : 'json',
					success : function(data) {
						console.log(data)
						var result = data.resultJson;
						if (result == '1') {
							window.location.reload()
						} else {

						}
					},
					error : function() {
					}
				});
			},
			onCancel : function(e) {

			}
		});
	});
})

$('#but-discuss').click(function() {
	var content = $.trim($('#content-discuss').val());
	var id = getQueryString('id');

	$.ajax({
		type : "post",
		async : true,
		url : "discussAddOfArticle",
		data : {
			'id' : id,
			'content' : content,
		},
		dataType : 'json',
		success : function(data) {
			console.log(data)

			var result = data.resultJson;

			if (result == '1') {
				window.location.href = "articleOne?id=" + data.id + "";
			} else {

			}

		},
		error : function(data) {
		}
	});

})
// 发布时间'2017-03-26T09:29:34' 返回"几天前"
function getDateDiff(dateTimeStamp) {
	dateTimeStamp = Date.parse(dateTimeStamp.replace(/-/gi, "/"));
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var diffValue = now - dateTimeStamp;
	if (diffValue < 0) {
		return;
	}
	var monthC = diffValue / month;
	var weekC = diffValue / (7 * day);
	var dayC = diffValue / day;
	var hourC = diffValue / hour;
	var minC = diffValue / minute;
	if (monthC >= 1) {
		result = "" + parseInt(monthC) + "月前";
	} else if (weekC >= 1) {
		result = "" + parseInt(weekC) + "周前";
	} else if (dayC >= 1) {
		result = "" + parseInt(dayC) + "天前";
	} else if (hourC >= 1) {
		result = "" + parseInt(hourC) + "小时前";
	} else if (minC >= 1) {
		result = "" + parseInt(minC) + "分钟前";
	} else
		result = "刚刚";
	return result;
}
